package org.thomias.exo3.map;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class PersistantMap implements Map<String, String> {
	private String dir;

	public PersistantMap(String dir) {
		this.dir = dir;
		File file = new File(dir);
		file.mkdirs();
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean containsKey(Object key) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String get(Object key) {
		FileInputStream f;
		String result;
		try {
			f = new FileInputStream(dir + "/" + key + ".map");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] read = new byte[1024];
			int len = 0;
			while ((len = f.read(read, 0, read.length)) != -1) {
				baos.write(read, 0, len);
			}
			result = new String(baos.toByteArray());
			f.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return result;
	}

	@Override
	public String put(String key, String value) {
		try {
			FileOutputStream outFile = new FileOutputStream(dir + "/" + key
					+ ".map");
			try {
				outFile.write(value.getBytes());
			} finally {
				outFile.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String remove(Object key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void putAll(Map<? extends String, ? extends String> m) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public Set<String> keySet() {
		File folder = null;
		Set<String> result = new LinkedHashSet<String>();
		try {
			folder = new File(dir);
			File[] fileList = folder.listFiles();
			Arrays.sort(fileList, new Comparator<File>() {
				@Override
				public int compare(File f1, File f2) {

					return f1.getName().compareTo(f2.getName());
				}
			});
			for (int i = 0; i < fileList.length; i++) {
				if (fileList[i].isFile()) {
					String fileName = fileList[i].getName();
					if (fileName.endsWith(".map")) {
						result.add(fileName.substring(0, fileName.length() - 4));
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Collection<String> values() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<java.util.Map.Entry<String, String>> entrySet() {
		// TODO Auto-generated method stub
		return null;
	}

}
